import tkinter as tk
import tkinter.ttk as ttk

root = tk.Tk()
root.geometry("800x600")  # 更大的初始窗口尺寸
root.title("运维工具 v0.0.1")

try:
    root.iconbitmap('static/img/logo.ico')
except tk.TclError:
    print("图标文件未找到，使用默认图标")

style = ttk.Style()

# 定义样式配置字典
style_configs = {
    "BW.TLabel": {"foreground": "black", "background": "white"},
    "RG.TLabel": {"foreground": "red", "background": "green"},
    "BY.TLabel": {"foreground": "blue", "background": "yellow"}
}

# 批量应用样式
for style_name, config in style_configs.items():
    style.configure(style_name, **config)

# 创建左右分割的布局
paned_window = tk.PanedWindow(root, orient=tk.HORIZONTAL, sashwidth=5, sashrelief=tk.RAISED)
paned_window.pack(fill=tk.BOTH, expand=True)

# 左边部分 - 固定宽度框架
left_frame = tk.Frame(paned_window, bg="lightblue", width=200)
left_frame.pack_propagate(True)  # 禁止子组件影响框架大小
paned_window.add(left_frame, minsize=200)  # 设置最小宽度

# 导入并创建TreeViewExample实例
from tkinter面板.pages.左边菜单列 import TreeViewExample

tree = TreeViewExample(left_frame)

# 右边部分 - 填充剩余空间
right_frame = tk.Frame(paned_window, bg="lightgreen")
paned_window.add(right_frame)  # 右侧也设置最小宽度

# 导入Notebooks类
from tkinter面板.pages.标签页管理 import Notebooks

# 创建两个框架，用于切换显示
home_frame = tk.Frame(right_frame, bg="lightgreen")
notebook_frame = tk.Frame(right_frame, bg="white")
isDel = tk.BooleanVar(notebook_frame, value=False)
# 两个框架都填充整个right_frame
for frame in [home_frame, notebook_frame]:
    frame.pack(fill=tk.BOTH, expand=True)
    frame.pack_forget()  # 先隐藏，后面再显示需要的

# 初始显示home_frame
home_frame.pack(fill=tk.BOTH, expand=True)

# 添加内容到home_frame
ttk.Label(home_frame, text="右侧区域", style='RG.TLabel').pack(pady=20)

# 创建一个变量用于存储Notebooks实例
notebook_instance = None


# 返回主页的函数
def return_to_home():
    # 隐藏notebook_frame和返回按钮
    notebook_frame.pack_forget()
    return_button.pack_forget()

    # 显示home_frame
    home_frame.pack(fill=tk.BOTH, expand=True)


# 打开Notebooks的函数
def open_notebooks():
    global notebook_instance

    # 隐藏home_frame
    home_frame.pack_forget()

    # 显示notebook_frame
    notebook_frame.pack(fill=tk.BOTH, expand=True)

    # 如果还没创建Notebooks实例，则创建它
    if notebook_instance is None:
        notebook_instance = Notebooks(notebook_frame, **{
            "isDel": isDel,
            "return_callback": return_to_home  # 修正：传递函数引用，而非调用函数
        })

    # 显示返回按钮
    return_button.pack(anchor='nw', pady=10, padx=10)


# 创建打开按钮
open_button = ttk.Button(
    home_frame,
    text='打开Notebooks',
    command=open_notebooks,
    style='BW.TLabel',
)
open_button.pack(pady=20)

# 创建返回按钮
return_button = ttk.Button(
    notebook_frame,
    text="返回主界面",
    command=return_to_home,
    style='BW.TLabel',
)
# 不立即显示返回按钮，等打开Notebooks时再显示

root.mainloop()
